package com.fled.itsmbigscreen.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class HmacSHA1Example {
    public static void main(String[] args) {
        try {
            String secretKey = "小太阳小月亮";
            String message = "你的行为和表现还是让我无法完全相信你放下了";

            // 创建HmacSHA1实例
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
            mac.init(secretKeySpec);

            // 计算HmacSHA1
            byte[] hmacBytes = mac.doFinal(message.getBytes(StandardCharsets.UTF_8));

            // 将结果转换为Base64编码
            String hmacBase64 = Base64.getEncoder().encodeToString(hmacBytes);

            System.out.println("HmacSHA1: " + hmacBase64);
            System.out.println(hmacBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}